package com.mljr.example.ui.home.tab1;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ctakit.sdk.app.widget.list.pullleft.PullLeftToRefreshLayout;
import com.ctakit.sdk.view.ViewInjector;
import com.ctakit.sdk.view.annotation.ViewInject;
import com.mljr.example.R;
import com.mljr.example.bean.tab1.Voucher;

import java.util.List;


/**
 * Created by andyhome on 16/4/20.
 */
public class HeaderOperationViewView extends HeaderViewInterface<List<Voucher>> {

    @ViewInject(R.id.rv)
    RecyclerView rv;
    @ViewInject(R.id.plrl)
    PullLeftToRefreshLayout plrl;

    private Context mContext ;

    public HeaderOperationViewView(Activity context) {
        super(context);
        mContext = context;
    }

    @Override
    protected void getView(List<Voucher> list, ListView listView) {
        View view = mInflate.inflate(R.layout.header_operation_layout, listView, false);
        ViewInjector.inject(this, view);

        dealWithTheView(list);
        listView.addHeaderView(view);
    }

    private void dealWithTheView(List<Voucher> list) {

        rv.setAdapter(new PullAdapter(list));
        rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

        plrl.setOnRefreshListener(new PullLeftToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(mContext, "刷新数据成功", Toast.LENGTH_SHORT).show();
            }
        });


    }



}

class PullAdapter extends RecyclerView.Adapter<PullAdapter.PullrHolder>{
    private List<Voucher> mData;
    @Override
   public PullrHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tab1_item_pull_left, parent, false);
       return new PullrHolder(view);
   }

    public PullAdapter(List<Voucher> data){
        this.mData = data;
    }

    @Override
   public void onBindViewHolder(PullrHolder holder, int position) {

        Voucher voucher =  mData.get(position);
        holder.name.setText(voucher.getMerchantName());
        holder.limitAmount.setText("消费满"+(int)voucher.getLimitAmount()+"可用");
        holder.time.setText("有效期："+voucher.getSdate()+"至"+voucher.getEdate());
        holder.amountLowerBound.setText((int)voucher.getAmountLowerBound()+"");

   }

   @Override
   public int getItemCount() {
       return mData.size();
   }

   public class PullrHolder extends RecyclerView.ViewHolder{

       @ViewInject(R.id.name)
       public TextView name;

       @ViewInject(R.id.time)
       public TextView time;
       @ViewInject(R.id.limitAmount)
       public TextView limitAmount;
       @ViewInject(R.id.amountLowerBound)
       public TextView amountLowerBound;

       public PullrHolder(View itemView) {

           super(itemView);
           ViewInjector.inject(this, itemView);
       }

       public TextView getName() {
           return name;
       }

       public void setName(TextView name) {
           this.name = name;
       }

       public TextView getTime() {
           return time;
       }

       public void setTime(TextView time) {
           this.time = time;
       }

       public TextView getLimitAmount() {
           return limitAmount;
       }

       public void setLimitAmount(TextView limitAmount) {
           this.limitAmount = limitAmount;
       }

       public TextView getAmountLowerBound() {
           return amountLowerBound;
       }

       public void setAmountLowerBound(TextView amountLowerBound) {
           this.amountLowerBound = amountLowerBound;
       }
   }

}
